<?php
namespace app\admin\builder\src;
use app\admin\builder\MBuilder;

class Tables extends MBuilder
{
    protected $config = [
        'action'=>'',
        'url'=>['index_url'=>'','add_url'=>'','edit_url'=>'','delete_url'=>'','export_url'=>'','modify_url'=>''],
        'type'=>'ordinary',
        'ispage'=>true,
        'totalRow'=>[
            'open'=>false,
            'totalRowText'=>'合计'
        ],
        'limits'=>[15,20,30,40,50,60,70,80,90,100,500,1000,5000,10000],
        'toolbar'=>[],
        'defaultToolbar'=>[],
        'row'=>[],
        'defaultOperat'=>[],
        'operat'=>[
            'width'=>200,
            'text'=>'操作',
            'button'=>[]
        ],
    ];

    /**设置数据请求地址
     * @param string $action
     * @return $this
     */
    public function setAction(string $action=''){
        if($action){
            $this->config['action'] = $action;
        }
        return $this;
    }

    /**设置各种url地址
     * @param array|string[] $url
     * @return $this
     */
    public function setUrl(array $url=['index_url'=>'','add_url'=>'','edit_url'=>'','delete_url'=>'','export_url'=>'','modify_url'=>'']){
        $this->config['url'] = $url;
        return $this;
    }

    /**表格类型
     * @param string(ordinary|tree) $type
     * @return $this
     */
    public function setType(string $type='ordinary'){
        $this->config['type'] = $type;
        return $this;
    }

    /**设置是否分页
     * @param bool $bool
     * @return $this
     */
    public function setPage(bool $bool=true){
        $this->config['ispage'] = $bool;
        return $this;
    }

    /**设置每页数量list
     * @param array|int[] $limits
     * @return $this
     */
    public function setLimits(array $limits=[15,20,30,40,50,60,70,80,90,100,500,1000,5000,10000]){
        $this->config['limits'] = $limits;
        return $this;
    }

    /**设置开启合计栏
     * @param bool $open
     * @param string $totalRowText
     * @return $this
     */
    public function setTotalRow(bool $open=true,$totalRowText='合计'){
        $this->config['totalRow']['open'] = $open;
        $this->config['totalRow']['totalRowText'] = $totalRowText;
        return $this;
    }

    /**设置默认的toolbar
     * @param string(refresh|delete|export) $str
     * @return $this
     */
    public function setDefaultToolbar(string $str){
        $this->config['defaultToolbar'][] = $str;
        return $this;
    }

    /**设置Toolbar
     * @param string显示名称 $title
     * @param string请求方式(open |request ) $method
     * @param string请求url $url
     * @param string权限 $auth
     * @param bool是否需要数据选择 $checkbox
     * @param string样式 $class
     * @param string标识 $icon
     * @return $this
     */
    public function setToolbar(string $title,string $method='request',string $url='',string $auth='',bool $checkbox=false,string $class='layui-btn layui-btn-normal layui-btn-sm',string $icon=''){
        $this->config['toolbar'][] = [
            'title'=>$title,
            'method'=>$method,
            'url'=>$url,
            'auth'=>$auth,
            'checkbox'=>$checkbox,
            'class'=>$class,
            'icon'=>$icon,
        ];
        return $this;
    }

    /**设置默认操作栏
     * @param string(edit|delete) $str
     * @return $this
     */
    public function setDefaultOperat(string $str){
        $this->config['defaultOperat'][] = $str;
        return $this;
    }

    /**设置操作栏
     * @param string名称 $title
     * @param string(open|request) $method
     * @param string请求地址 $url
     * @param string权限 $auth
     * @param string绑定字段 $field
     * @param string样式 $class
     * @param string $icon
     * @return $this
     */
    public function setOperat(string $title,string $method='request',string $url='',string $auth='',string $field='id',string $class='layui-btn layui-btn-xs layui-btn-normal',string $icon=''){
        $this->config['operat']['button'][] = [
            'title'=>$title,
            'method'=>$method,
            'url'=>$url,
            'auth'=>$auth,
            'field'=>$field,
            'class'=>$class,
            'icon'=>$icon,
        ];
        return $this;
    }

    /**设置操作宽度
     * @param int $width
     * @param string $text
     * @return $this
     */
    public function setUpOperat(int $width=200,string $text='操作'){
        $this->config['operat']['width'] = $width;
        $this->config['operat']['text'] = $text;
        return $this;
    }

    /**设置表单
     * @param string字段 $field
     * @param string字段名 $title
     * @param false搜索(false|true|select|range|time) $search
     * @param array(select的选项) $selectList
     * @param bool(开启合计) $totalRow
     * @param string(模板) $templet
     * @param int(宽度) $width
     * @param int(最小宽度) $minWidth
     * @return $this
     */
    public function setRow(string $field,string $title='',$search=false,array $selectList=[],bool $totalRow=false,string $templet='',int $width=200,int $minWidth=200){
//        if(empty($selectList)){
//            $selectList = '';
//        }else{
//            $selectList = json_encode($selectList,JSON_UNESCAPED_UNICODE |JSON_UNESCAPED_SLASHES);
//        }
        $this->config['row'][] = [
            'field'=>$field,
            'title'=>$title,
            'search'=>$search,
            'selectList'=>$selectList,
            'totalRow'=>$totalRow,
            'templet'=>$templet,
            'width'=>$width,
            'minWidth'=>$minWidth,
        ];
        return $this;
    }
    public function fetch(string $emplate = '')
    {
//        $this->config['limits'] = json_encode($this->config['limits'],JSON_UNESCAPED_UNICODE |JSON_UNESCAPED_SLASHES);
//        echo '<pre>';
//        print_r($this->config);
//        exit;
        return view(app_path().'/view/builder/table/index.html',['config'=>json_encode($this->config,JSON_UNESCAPED_UNICODE |JSON_UNESCAPED_SLASHES),'thisControllerJsPath'=>'admin/js/builder/form.js','thisAction'=>'index','formname'=>'ces.html']); // TODO: Change the autogenerated stub
    }
    public function json($array){
        return json_encode($array,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
    }
}